package oop.db.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import oop.data.TopicConstraint;
import oop.db.Database;

public class TopicConstraintDAO {

	public static TopicConstraint create(long sectionStructureId, long topicId,
			int count) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			String sql = "INSERT INTO tblTopicConstraint (sstr_id, top_id, count) " +
					"VALUES (?, ?, ?)";
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setLong(1, sectionStructureId);
			stmt.setLong(2, topicId);
			stmt.setInt(3, count);
			stmt.executeUpdate();
			return new TopicConstraint(sectionStructureId, topicId, count);
		} finally {
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static void drop(long sectionStructureId, long topicId)
			throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			String sql = "DELETE FROM tblTopicConstraint " +
					"WHERE sstr_id = ? AND top_id = ?";
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setLong(1, sectionStructureId);
			stmt.setLong(2, topicId);
			stmt.executeUpdate();
		} finally {
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static void persist(TopicConstraint constraint) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			String sql = "UPDATE tblTopicConstraint " +
					"SET count = ?" +
					"WHERE sstr_id = ? AND top_id = ?";
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setLong(1, constraint.getSectionStructure().getId());
			stmt.setLong(2, constraint.getTopic().getId());
			stmt.setInt(3, constraint.getCount());
			stmt.executeUpdate();
		} finally {
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static List<TopicConstraint> fetchBySectionStructure(
			long sectionStructureId) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT sstr_id, top_id, count " +
					"FROM tblTopicConstraint " +
					"WHERE sstr_id = ?";
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setLong(1, sectionStructureId);
			rs = stmt.executeQuery();
			return fromResultSet(rs);
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static List<TopicConstraint> fromResultSet(ResultSet rs)
			throws SQLException {
		List<TopicConstraint> answers = new ArrayList<TopicConstraint>();
		while (rs.next()) { 
			TopicConstraint constraint = new TopicConstraint(rs.getLong(1), rs
					.getLong(2), rs.getInt(3));
			answers.add(constraint);
		}
		return answers;
	}
	
}
